package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.fellingtrees.*;
import com.yanqu.road.entity.activity.fellingtrees.config.FellingTreesConfig;
import com.yanqu.road.entity.activity.fellingtrees.config.FellingTreesElixirConfig;
import com.yanqu.road.entity.activity.fellingtrees.config.FellingTreesLevelConfig;
import com.yanqu.road.entity.activity.fellingtrees.config.FellingTreesMaterialsConfig;
import com.yanqu.road.entity.activity.fellingtrees.config.FellingTreesWeaponsConfig;
import com.yanqu.road.pb.activity.FellingTreesProto;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class FellingTreesPb {

    public static FellingTreesProto.FellingTreesUserDataMsg.Builder parseFellingTreesUserDataMsg(FellingTreesUserData data) {
        FellingTreesProto.FellingTreesUserDataMsg.Builder builder = FellingTreesProto.FellingTreesUserDataMsg.newBuilder();
        builder.setGoldCoin(data.getGold().longValue());
        builder.setTopTreeLevel(data.getTopLevel());
        builder.setEnergy(data.getEnergy());
        builder.setEnergyRecovery(data.getRecoveryTime());
        builder.setAttack(data.getAttack().longValue());
        for (Map.Entry<Integer, Integer> entry : data.getBuffMap().entrySet()) {
            builder.addBuff(FellingTreesProto.FellingTreesBuffMsg.newBuilder()
                    .setBuffId(entry.getKey())
                    .setTimes(entry.getValue())
            );
        }
        builder.setRabbitEnergy(data.getRabbitEnergy());
        builder.setRabbitEnergyRecovery(data.getRabbitRecoveryTime());
        builder.setLastLevel(data.getLastLevel());
        builder.setLastHp(data.getLastHp().longValue());
        builder.addAllPos(parseFellingTreesPosMsgList(data.getEquipPosArr()));
        builder.setRabbitGuide(data.isRabbitGuide());
        builder.setSpecialEquipGuide(data.isSpecialEquipGuide());
        builder.setDaoqiGuide(data.isDaoqiGuide());
        return builder;
    }

    public static FellingTreesProto.FellingTreesEquipConfigMsg.Builder parseFellingTreesEquipConfigMsg(FellingTreesWeaponsConfig config) {
        FellingTreesProto.FellingTreesEquipConfigMsg.Builder builder = FellingTreesProto.FellingTreesEquipConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setType(config.getType());
        builder.setIcon(config.getIcon());
        builder.setQuality(config.getQuality());
        builder.setBaseAttack(config.getBaseAttack());
        builder.setEffectAttack(config.getEffectAttack());
        builder.setEffectMode(config.getEffectMode());
        builder.setEffectAttack(config.getEffectAttack());
        builder.setDropLevel(config.getDropLevel());
        builder.setWeight(config.getWeight());
        builder.setParam(config.getParam());
        return builder;
    }

    public static FellingTreesProto.FellingTreesElixirConfigMsg.Builder parseFellingTreesElixirConfigMsg(FellingTreesElixirConfig config) {
        FellingTreesProto.FellingTreesElixirConfigMsg.Builder builder = FellingTreesProto.FellingTreesElixirConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setIcon(config.getIcon());
        builder.setQuality(config.getQuality());
        builder.setType(config.getType());
        builder.setParam(config.getParam());
        builder.setTimes(config.getTimes());
        builder.setEnergy(config.getEnergy());
        builder.setMaterials(config.getMaterials());
        return builder;
    }

    public static FellingTreesProto.FellingTreesLevelConfigMsg.Builder parseFellingTreesLevelConfigMsg(FellingTreesLevelConfig config) {
        FellingTreesProto.FellingTreesLevelConfigMsg.Builder builder = FellingTreesProto.FellingTreesLevelConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setLevel(config.getLevel());
        builder.setHp(config.getHp().toString());
        builder.setGold(config.getGold().toString());
        builder.setDropLevel(config.getDropLevel());
        builder.setWeaponAtt(config.getWeaponAtt().toString());
        builder.setSellGold(config.getSellGold().toString());
        builder.setStrengthGold(config.getStrengthGold().toString());
        return builder;
    }

    public static FellingTreesProto.FellingTreesItemConfigMsg.Builder parseFellingTreesItemConfigMsg(FellingTreesMaterialsConfig config) {
        FellingTreesProto.FellingTreesItemConfigMsg.Builder builder = FellingTreesProto.FellingTreesItemConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setIcon(config.getIcon());
        builder.setQuality(config.getQuality());
        builder.setDropLevel(config.getDropLevel());
        builder.setWeight(config.getWeight());
        return builder;
    }

    public static FellingTreesProto.FellingTreesSyncConfigRespMsg.Builder parseFellingTreesSyncConfigRespMsg(FellingTreesConfig config) {
        FellingTreesProto.FellingTreesSyncConfigRespMsg.Builder builder = FellingTreesProto.FellingTreesSyncConfigRespMsg.newBuilder();
        for (FellingTreesWeaponsConfig value : config.getWeaponsConfigMap().values()) {
            builder.addEquipConfig(parseFellingTreesEquipConfigMsg(value));
        }
        for (FellingTreesElixirConfig value : config.getElixirConfigMap().values()) {
            builder.addElixirConfig(parseFellingTreesElixirConfigMsg(value));
        }
        for (FellingTreesMaterialsConfig value : config.getMaterialsConfigMap().values()) {
            builder.addItemConfig(parseFellingTreesItemConfigMsg(value));
        }
        for (FellingTreesLevelConfig value : config.getLevelConfigMap().values()) {
            builder.addLevelConfig(parseFellingTreesLevelConfigMsg(value));
        }
        return builder;
    }

    public static FellingTreesProto.FellingTreesRewardMsg.Builder parseFellingTreesRewardMsg(FellingTreesConfig config, FellingTreesReward data) {

        List<FellingTreesEquip> tmpEquipList = new ArrayList<>();
        Property tmpReward = new Property();

        // 汇总奖励
        tmpReward.addProperty(data.getItems()); // 材料
        tmpReward.addProperty(config.getGoldItemId(), data.getGold()); // 金币
        tmpEquipList.addAll(data.getNormalEquipList()); // 法宝
        for (Map.Entry<Integer, BigInteger> entry : data.getSpecialEquips().getGoods().entrySet()) {
            for (int i = 0; i < entry.getValue().longValue(); i++) {
                tmpEquipList.add(FellingTreesEquip.createSpecialEquip(entry.getKey())); // 灵宝升级材料转灵宝装备
            }
        }
        for (Map.Entry<Integer, BigInteger> entry : data.getDaoQi().getGoods().entrySet()) {
            for (int i = 0; i < entry.getValue().longValue(); i++) {
                tmpEquipList.add(FellingTreesEquip.createSpecialEquip(entry.getKey())); // 道器转装备
            }
        }

        String reward = PropertyHelper.parsePropertyToString(tmpReward);

        // 返回
        FellingTreesProto.FellingTreesRewardMsg.Builder builder = FellingTreesProto.FellingTreesRewardMsg.newBuilder();
        builder.setLevel(data.getLv());
        builder.setReward(reward);
        builder.addAllEquip(parseFellingTreesEquipMsgList(tmpEquipList));
        return builder;
    }

    public static FellingTreesProto.FellingTreesEquipMsg.Builder parseFellingTreesEquipMsg(FellingTreesEquip data) {
        FellingTreesProto.FellingTreesEquipMsg.Builder builder = FellingTreesProto.FellingTreesEquipMsg.newBuilder();
        builder.setUniqueId(data.getUid());
        builder.setEquipId(data.getEquipId());
        builder.setLevel(data.getLv());
        builder.setLock(data.isLock());
        builder.setBaseLevel(data.getBaseLv());
        for (FellingTreesEquipParam equipParam : data.getParamList()) {
            FellingTreesProto.FellingTreesEquipParamMsg.Builder paramMsg = parseFellingTreesEquipParamMsg(equipParam);
            if (paramMsg != null) {
                builder.addParam(paramMsg);
            }
        }
        return builder;
    }

    public static FellingTreesProto.FellingTreesEquipParamMsg.Builder parseFellingTreesEquipParamMsg(FellingTreesEquipParam data) {
        if (data == null) {
            return null;
        }
        FellingTreesProto.FellingTreesEquipParamMsg.Builder builder = FellingTreesProto.FellingTreesEquipParamMsg.newBuilder();
        builder.setType(data.getType());
        builder.setValue(data.getValue());
        return builder;
    }


    public static List<FellingTreesProto.FellingTreesPosMsg> parseFellingTreesPosMsgList(long[] equipPosArr) {
        List<FellingTreesProto.FellingTreesPosMsg> list = new ArrayList<>();
        int index = 0;
        for (long id : equipPosArr) {
            FellingTreesProto.FellingTreesPosMsg.Builder builder = FellingTreesProto.FellingTreesPosMsg.newBuilder();
            builder.setIndex(index++);
            builder.setUniqueId(id);
            list.add(builder.build());
        }
        return list;
    }

    public static FellingTreesProto.FellingTreesSettingMsg.Builder parseFellingTreesSettingMsg(FellingTreesSetting data) {
        FellingTreesProto.FellingTreesSettingMsg.Builder builder = FellingTreesProto.FellingTreesSettingMsg.newBuilder();
        builder.setAutoEquip(data.isAutoEquip());
        builder.setAutoRecycle(data.isAutoRecycle());
        return builder;
    }

    public static List<FellingTreesProto.FellingTreesItemMsg> parseFellingTreesItemMsgList(Property property) {
        List<FellingTreesProto.FellingTreesItemMsg> list = new ArrayList<>();
        for (Map.Entry<Integer, BigInteger> entry : property.getGoods().entrySet()) {
            FellingTreesProto.FellingTreesItemMsg.Builder builder = FellingTreesProto.FellingTreesItemMsg.newBuilder();
            builder.setItemId(entry.getKey());
            builder.setNum(entry.getValue().longValue());
            list.add(builder.build());
        }
        return list;
    }

    public static List<FellingTreesProto.FellingTreesEquipMsg> parseFellingTreesEquipMsgList(Collection<FellingTreesEquip> equips) {
        List<FellingTreesProto.FellingTreesEquipMsg> list = new ArrayList<>();
        for (FellingTreesEquip equip : equips) {
            list.add(parseFellingTreesEquipMsg(equip).build());
        }
        return list;
    }
}
